﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XWord.WPF.ApplicationFramework.Extensions;
namespace AppDemo
{
    /// <summary>
    /// Interaction logic for Window5.xaml
    /// </summary>
    public partial class Window5 : Window
    {
        RectangleElement dv;
        public Window5()
        {
            InitializeComponent();
          //  this.Content = new RectangleElement();
        }

        void Window5_Loaded(object sender, RoutedEventArgs e)
        {
            //var a = new SigletonVisual();
        }

       

        //protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        //{
        //    var drawingContext = dv.RenderOpen();
        //    drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, this.ActualWidth, ActualHeight));
        //    drawingContext.Close();
        //    base.OnRenderSizeChanged(sizeInfo);
        //}

        //protected override Visual GetVisualChild(int index)
        //{
        //    return dv;
        //}

        //protected override int VisualChildrenCount
        //{
        //    get
        //    {
        //        return 1;
        //    }
        //}
    }

    public class RectangleElement : UIElement
    {
        DrawingVisual visual; 
        public RectangleElement()
        {
            visual = new DrawingVisual();
            var drawingContext = visual.RenderOpen();
            drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 20));
            drawingContext.Close();
          //  this.AddVisualChild(visual);
        }

        protected override Visual GetVisualChild(int index)
        {
            return visual;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }
    }

    public class SigletonVisual : Visual
    {
        public SigletonVisual()
        {
            DrawingVisual dv = new DrawingVisual();
            var drawingContext = dv.RenderOpen();
            drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 20));
            this.Child = dv;
            drawingContext.Close();
        }

        public Visual _child;
        public Visual Child
        {
            get
            {
                return _child;
            }
            set
            {
               
              //  this.AddVisualChild(value);
                _child = value;
            }
        }



        protected override Visual GetVisualChild(int index)
        {
            return _child;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                if (this._child != null)
                {
                    return 1;
                }
                return 0;
            }
        }
    }

    //protected override void OnRender(DrawingContext drawingContext)
    //{
    //    drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 20));
    //    base.OnRender(drawingContext);
    //}

    public class RectangleVisual : ContainerVisual
    {
        public RectangleVisual()
        {
            DrawingVisual dv = new DrawingVisual();
            var drawingContext = dv.RenderOpen();
            drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 100, 20));
            this.Children.Add(dv);
            drawingContext.Close();
        } 
    }
}
